{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "dc5c0ada",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "cannot assign to operator (19431830.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"/var/folders/mj/gzzjyp5s3j5fq8jywxpy4dyc0000gn/T/ipykernel_20769/19431830.py\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m    str = appId +\"-\"+ now +\"-\"+ appSecret = 1abcdef\u001b[0m\n\u001b[0m          ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m cannot assign to operator\n"
     ]
    }
   ],
   "source": [
    "str = appId +\"-\"+ now +\"-\"+ appSecret = 1abcdef"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4c13f441",
   "metadata": {},
   "outputs": [],
   "source": [
    "import hashlib\n",
    "\n",
    " \n",
    "#sha1（大写）加密\n",
    "\n",
    "def get_str_sha1_secret_str(res:str):\n",
    "\n",
    "     #使用sha1加密算法，返回str加密后的字符串\n",
    "\n",
    "     sha = hashlib.sha1(res.encode('utf-8'))\n",
    "\n",
    "     #将字节码转为16进制\n",
    "\n",
    "     encrypts = sha.hexdigest()\n",
    "\n",
    "     #将16进制字符全部转换为大写格式（如需要用到大写加密才需要此步）\n",
    "\n",
    "     upper_encrypts = encrypts.upper()\n",
    "\n",
    "     return upper_encrypts\n",
    "\n",
    " \n",
    "\n",
    "#MD5加密\n",
    "\n",
    "def get_str_md5_secret_str(res:str):\n",
    "\n",
    " \n",
    "\n",
    "     #使用md5加密算法，返回str加密后的字符串\n",
    "\n",
    "     md5 = hashlib.md5(res.encode('utf-8'))\n",
    "\n",
    "     #将字节码转为16进制\n",
    "\n",
    "     encrypts = md5.hexdigest()\n",
    "\n",
    "     return encrypts\n",
    "\n",
    " \n",
    "\n",
    " \n",
    "\n",
    "# MD5加盐加密，多封装一个值，提高安全性\n",
    "\n",
    "def get_str_md5salt_secret_str(res:str, salt):\n",
    "\n",
    "     #传的字符串应是字符串和盐值，用加，乘等都可以，salt可以是时间戳、或任意值\n",
    "\n",
    "     res = res + salt\n",
    "\n",
    "     #使用md5加密算法，返回str加密后的字符串\n",
    "\n",
    "     md5 = hashlib.md5(res.encode('utf-8'))\n",
    "\n",
    "     #将字节码转为16进制\n",
    "\n",
    "     encrypts = md5.hexdigest()\n",
    "\n",
    "     return encrypts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f8a4dca6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8c7373a816923639d248bf71d83f68b608a3b615.1576207584442\n"
     ]
    }
   ],
   "source": [
    "appId=12345\n",
    "appSecret='abcdef'\n",
    "now=1576207584442\n",
    "fmt_str='{appId}-{now}-{appSecret}'\n",
    "astr=get_str_sha1_secret_str(fmt_str.format(\n",
    "    appId=appId\n",
    "    ,appSecret=appSecret\n",
    "    ,now=now\n",
    "))\n",
    "print('{}.{}'.format(astr.lower(),now))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "dfe2c3b2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a4e3609a030cac542eb5efdcb1b8ccaf30279373.1669980442000\n"
     ]
    }
   ],
   "source": [
    "appId=1122\n",
    "appSecret='iee88iksd8syzuon27b46lwhilpn8bxt'\n",
    "now=1669980442000\n",
    "fmt_str='{appId}-{now}-{appSecret}'\n",
    "astr=get_str_sha1_secret_str(fmt_str.format(\n",
    "    appId=appId\n",
    "    ,appSecret=appSecret\n",
    "    ,now=now\n",
    "))\n",
    "print('{}.{}'.format(astr.lower(),now))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81a7f52d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
